﻿#include "YCameraConfig.h"
#include <fstream>


//插入摄像机 线程安全
void YCameraConfig::Push(const YCameraData& data)
{
    std::unique_lock<std::mutex> lock(mux_);
    cams_.push_back(data);
}
int YCameraConfig::GetCameraCount()
{
    std::unique_lock<std::mutex> lock(mux_);
    return cams_.size();
}
YCameraData YCameraConfig::GetCamera(int index)
{
    std::unique_lock<std::mutex> lock(mux_);
    if (index<0 || index> cams_.size())
        return YCameraData();
    return cams_[index];
}
bool YCameraConfig::DelCamera(int index)
{
    std::unique_lock<std::mutex> lock(mux_);
    if (index<0 || index> cams_.size())
        return false;
    cams_.erase(cams_.begin() + index);
    return true;
}
bool YCameraConfig::ModifyCamera(int index, const YCameraData& data)
{
    std::unique_lock<std::mutex> lock(mux_);
    if (index<0 || index> cams_.size())
        return false;
    cams_[index] = data;
    return true;
}
bool YCameraConfig::Load(const char* path)
{
    if (!path)return false;
    std::ifstream ifs(path, std::ios::binary);
    if (!ifs)return false;
    YCameraData data;
    std::unique_lock<std::mutex> lock(mux_);
    cams_.clear();
    for (;;)
    {
        ifs.read((char*)&data, sizeof(data));
        if (ifs.gcount() != sizeof(data))
        {
            ifs.close();
            return true;
        }
        cams_.push_back(data);
    }
    ifs.close();
    return true;
}
bool YCameraConfig::Save(const char* path)
{
    if (!path)return false;
    std::ofstream ofs(path, std::ios::binary);
    if (!ofs)return false;
    std::unique_lock<std::mutex> lock(mux_);
    for (auto cam : cams_)
    {
        ofs.write((char*)&cam, sizeof(cam));
    }
    ofs.close();
    return true;
}

YCameraConfig::~YCameraConfig()
{
}
